package 宝贝复制;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import http.Http;

import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

/**
 * Created by User on 2017/11/27.
 */
public class Test {

    public static void main(String[] args) throws IOException {


        Map<String, String> cidAndKeyword = new HashMap<>();

        cidAndKeyword.put("1623", "半身裙");
        cidAndKeyword.put("1629", "大码女装");
        cidAndKeyword.put("162103", "毛衣");
        cidAndKeyword.put("162104", "衬衫");
        cidAndKeyword.put("162205", "牛仔裤");
        cidAndKeyword.put("50000671", "T恤");
        cidAndKeyword.put("50000697", "毛针织衫");
        cidAndKeyword.put("50000852", "中老年女装");
        cidAndKeyword.put("50008897", "西装");
        cidAndKeyword.put("50008898", "卫衣/绒衫");
        cidAndKeyword.put("50008899", "羽绒服");
        cidAndKeyword.put("50008900", "棉衣/棉服");
        cidAndKeyword.put("50008901", "风衣");
        cidAndKeyword.put("50008905", "皮草");
        cidAndKeyword.put("50010850", "连衣裙");
        cidAndKeyword.put("50011277", "短外套");
        cidAndKeyword.put("50013194", "毛呢外套");
        cidAndKeyword.put("50013196", "马夹");
        cidAndKeyword.put("121412004", "背心吊带");
        cidAndKeyword.put("121434004", "抹胸");


        cidAndKeyword.put("50012027", "低帮鞋");
        cidAndKeyword.put("50012028", "靴子");
        cidAndKeyword.put("50012032", "凉鞋");
        cidAndKeyword.put("50012033", "拖鞋");
        cidAndKeyword.put("50012042", "帆布鞋");
        cidAndKeyword.put("50012047", "雨鞋");
        cidAndKeyword.put("50012825", "高帮鞋");

        cidAndKeyword.put("50012018", "钱包卡套");
        cidAndKeyword.put("50012019", "旅行箱");
        cidAndKeyword.put("50050199", "旅行袋");
        cidAndKeyword.put("121368002", "证件包");
        cidAndKeyword.put("121384005", "手机包");
        cidAndKeyword.put("121392003", "钥匙包");
        cidAndKeyword.put("121400005", "卡包");
        cidAndKeyword.put("121400006", "卡套");
        cidAndKeyword.put("121434005", "钱包");
        cidAndKeyword.put("122654005", "男士包袋");

        cidAndKeyword.put("3035", "休闲裤");
        cidAndKeyword.put("50001748", "民族服装");
        cidAndKeyword.put("50005867", "工装制服");
        cidAndKeyword.put("50010160", "西服");
        cidAndKeyword.put("50010402", "Polo衫");
        cidAndKeyword.put("50011127", "皮裤");
        cidAndKeyword.put("50011129", "西裤");
        cidAndKeyword.put("50011130", "西服套装");
        cidAndKeyword.put("50011153", "背心/马甲");
        cidAndKeyword.put("50011165", "棉衣");
        cidAndKeyword.put("50025883", "呢大衣");
        cidAndKeyword.put("50025884", "羽绒裤");
        cidAndKeyword.put("50025885", "棉裤");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = simpleDateFormat.format(new Date());

        List<Map> res = new ArrayList<>();
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("C:\\Users\\User\\IdeaProjects\\zql\\src\\main\\resources\\json1111"+format+".txt"));

        for (Map.Entry<String, String> entry : cidAndKeyword.entrySet()) {
            String cid = entry.getKey();
            String keyword = entry.getValue();
            List itemIds = getItemIdsByKeyWords(keyword);


            ///////////////////////////////////////////////////
            Map logMap = new HashMap<>();
            logMap.put("cid", cid);
            logMap.put("keyword", keyword);
            logMap.put("itemIds", itemIds);
            String logMapJson = JSON.toJSONString(logMap) + "\r\n";
            System.out.println(logMapJson);
            out.write(logMapJson.getBytes());
            out.flush();
            ///////////////////////////////////////////////////


            for (Object id : itemIds) {
                HashMap hashMap = new HashMap();
                hashMap.put("sid", "6101609f77d30c7d017d07360f264326528448f7eee0e6179742176");
                hashMap.put("type", 2);
                hashMap.put("numIid", id);
                hashMap.put("cid", cid);
                res.add(hashMap);
            }

        }

        out.close();


        String json = JSON.toJSONString(res);
        System.out.println(json);

        FileOutputStream out2 = new FileOutputStream("C:\\Users\\User\\IdeaProjects\\zql\\src\\main\\resources\\json2222"+format+".txt");
        out2.write(json.getBytes());
        out2.close();


    }

    private static void test1() {
        //卫衣 T恤  外套
        //连衣裙  披风   豆奶粉   奶片    糖果     儿童椅   沙发   保温奶瓶    电动牙刷   学步车   化妆镜  电动机  变压器
        // 床单  被套   防护条   gps充电器   钢琴     琉璃    帆布鞋  单肩背包  笔记本电脑

        Long[] 卫衣 = new Long[]{556643949598L, 560424976306L, 559985811696L, 555598370515L, 556538738737L, 537592921907L, 524597206694L, 550954993750L, 537520572181L, 537981406263L, 537420298382L, 537380271235L, 556677613289L, 558438879742L, 536271228830L, 557210960948L, 560895263139L, 559149442580L, 555687823806L, 556748059492L};
        Long[] T恤 = new Long[]{556245136967L, 546169203000L, 559627076306L, 533756229072L, 554480568871L, 555963445002L, 527336143892L, 556681825515L, 556087536458L, 37558395984L, 557698781955L, 554587211777L, 558898609712L, 543362486660L, 540583918941L, 544388199865L, 561610482371L, 558462619217L, 43591014535L, 558145050729L};
        Long[] 外套 = new Long[]{558794509820L, 559834684231L, 532038238315L, 558152065314L, 557870942401L, 545108969833L, 560442178589L, 552831126375L, 560673716502L, 528815038154L, 555428615325L, 551147176315L, 559994101657L, 557646855562L, 558625493248L, 559725737522L, 561485415282L, 560112896216L, 560569651978L, 560066332141L};
        Long[] 连衣裙 = new Long[]{561032807439L, 559348482036L, 557778205935L, 530390029381L, 552767150783L, 560934040655L, 554826844051L, 45764399123L, 552655234033L, 555775687965L, 558621892330L, 539696183059L, 560305937709L, 545419230029L, 528485290575L, 559819638045L, 560084645946L, 560148009178L, 556378290496L, 559873806531L};
        Long[] 披风 = new Long[]{540471975800L, 543087539498L, 527127156039L, 551008007473L, 525334485770L, 543151019294L, 535721443330L, 550917063003L, 559070364227L, 546943179265L, 552544617416L, 558649988837L, 544054586306L, 543363190017L, 558210275837L, 1270924506L, 552544617416L, 540669839279L, 547535960788L, 39308276474L};
        Long[] 豆奶粉 = new Long[]{551232986442L, 544783388139L, 35797160459L, 537656329265L, 524392619036L, 561503262018L, 538209552741L, 551504760120L, 546765718582L, 536258886328L, 559024019395L, 1112120048L, 530713676715L, 524392619036L, 550008097958L, 39131500059L, 547518139270L, 539743652163L, 559580919527L, 559363194449L};
        Long[] 奶片 = new Long[]{520535395164L, 530823065851L, 529587469461L, 525268657029L, 531469820865L, 1206411839L, 43190556065L, 528219367511L, 556742377570L, 531830976618L, 542588895308L, 556294161679L, 44432845568L, 555661360590L, 37078617129L, 556832891682L, 555092291456L, 530326864555L, 546727973553L, 43081405126L};
        Long[] 糖果 = new Long[]{40705803268L, 42015740583L, 544856426887L, 541500022492L, 554831695546L, 543327388612L, 522865420455L, 546406940875L, 541057226064L, 561274649008L, 522193294998L, 558696711910L, 45418928574L, 557892302935L, 556810864294L, 537264933237L, 560046283698L, 965794648L, 560217492724L, 41853226927L};
        Long[] 儿童椅 = new Long[]{1244424138L, 43853545483L, 541874950652L, 522787188869L, 556274921178L, 558443064444L, 521353887795L, 529108602960L, 43660811069L, 552576121837L, 552895213121L, 547199225470L, 552732894476L, 549114616215L, 552435951188L, 528441708132L, 557628433965L, 42188562917L, 547316703091L, 557925750989L,};
        Long[] 沙发 = new Long[]{45577508901L, 560085334694L, 545027857550L, 539388637489L, 43126005361L, 534504140961L, 559940487376L, 550463505663L, 561218778128L, 41373916140L, 558850681749L, 557941643621L, 557426470889L, 548329349488L, 546969026423L, 539497454943L, 43317813083L, 558582397568L, 555076321405L, 551506411712L};
        Long[] 保温奶瓶 = new Long[]{543164134859L, 42175975983L, 540334608932L, 533251765964L, 540065938720L, 561312449027L, 45086611790L, 40305987407L, 546828693294L, 560272873454L, 561490551225L, 560383076189L, 540065938720L, 536974066742L, 556606379225L, 533599687751L, 560344183850L, 43462487241L, 534677984372L, 561312449027L};
        Long[] 电动牙刷 = new Long[]{561700307440L, 559318191847L, 522934179787L, 521774970730L, 561851090381L, 550974698011L, 561373821972L, 549315132361L, 555639377189L, 560675351240L, 543821964516L, 561461330374L, 537791594464L, 522696940864L, 40810376091L, 43370146787L, 546216513626L, 557038509508L, 553275662408L, 43222871298L};
        Long[] 学步车 = new Long[]{549431293869L, 531232628732L, 525687123337L, 533064344094L, 561605866278L, 540278264708L, 559460899158L, 520224221185L, 555273975730L, 544079273453L, 533699400961L, 540188397882L, 553819761491L, 45801855910L, 547941901662L, 525572745538L, 561398511711L, 40505339783L, 555218666301L, 539534149501L};
        Long[] 化妆镜 = new Long[]{552845842019L, 528613240233L, 534569820253L, 561528180686L, 533962569899L, 528411500734L, 560377430170L, 525777880418L, 35595226597L, 533262548825L, 524207949600L, 535383013440L, 533900189822L, 39831863451L, 525805603142L, 526513660150L, 543754347126L, 36992983642L, 556871785200L, 556811936315L};
        Long[] 电动机 = new Long[]{556177261958L, 523998536776L, 549234274413L, 559208756979L, 530477772486L, 546335216981L, 546296571317L, 1228775164L, 1163868828L, 38885693862L, 525026109817L, 552936514102L, 535915001539L, 535958548245L, 536636816345L, 536636324038L, 555160891484L, 524000405161L, 521089430157L, 561287481804L};
        Long[] 变压器 = new Long[]{531986283L, 35226265154L, 544059020748L, 549431837534L, 536915304420L, 528822317748L, 1160928418L, 550926576713L, 560116080306L, 40038455132L, 550560974160L, 561989959206L, 523392424717L, 38585490686L, 547239864762L, 1243219653L, 525492627857L, 555740929862L, 1277371833L, 554429369807L};
        Long[] 床单 = new Long[]{43464858439L, 546928045633L, 558908739713L, 559917014514L, 552858069688L, 551414167397L, 539640587036L, 525392326892L, 557561371586L, 536901577509L, 521540297814L, 520981104343L, 553181168461L, 546643609428L, 38177599250L, 534552246001L, 42534191085L, 543734688558L, 524597183151L, 538169562227L};
        Long[] 被套 = new Long[]{522015740702L, 558919943131L, 557822541927L, 559817035397L, 559235884499L, 541509144415L, 560394854851L, 560450656586L, 541982570593L, 558611551297L, 526258825736L, 521302844704L, 521206320146L, 44974288331L, 559549757304L, 559020663906L, 559065683400L, 524597183151L, 557308771666L, 561221192844L};
        Long[] 防护条 = new Long[]{45120261904L, 552923977778L, 1289724318L, 915417602L, 45638678318L, 45796721717L, 42294622363L, 555042186053L, 542525847651L, 1184995446L, 561503651202L, 550603853175L, 550603853175L, 1137195822L, 43856495545L, 45120913164L, 529706919130L, 551663119326L, 555841336411L, 543051918160L};
        Long[] gps充电器 = new Long[]{560168446353L, 557564971283L, 549620405703L, 555682969130L, 537727320627L, 1190823196L, 44673272153L, 554270110301L, 45080299497L, 520512640066L, 539963201908L, 531662498677L, 558297865519L, 523942319924L, 41296244355L, 39073492704L, 41073459209L, 520506096308L, 558315239102L, 534810855581L};
        Long[] 钢琴 = new Long[]{549057018823L, 558643537973L, 545766108952L, 543161026747L, 541244362380L, 44403479109L, 1278211742L, 531912643623L, 558753355496L, 536597636777L, 532974349448L, 557099757647L, 529090952966L, 548514209343L, 45857096643L, 38645442732L, 524824397372L, 544788772369L, 558659699353L, 556328868005L};
        Long[] 琉璃 = new Long[]{558994090044L, 544776876328L, 558704556727L, 524064420728L, 532937949244L, 35835033940L, 555728768493L, 556680877219L, 39458498817L, 939103118L, 538328009869L, 557936590466L, 560888049578L, 531543855998L, 536735932950L, 536045625783L, 42114361349L, 41306946780L, 40568433039L, 554907474378L};
        Long[] 帆布鞋 = new Long[]{532854190263L, 555115713311L, 536416453409L, 559047022769L, 38837043311L, 559726282715L, 43130947526L, 559311133668L, 553418107705L, 555882332940L, 561563390931L, 559460826079L, 559868169188L, 555700445993L, 37510173558L, 559855111870L, 560541891589L, 556896871961L, 549125142438L, 39509841324L};
        Long[] 单肩背包 = new Long[]{552281261424L, 41270164522L, 544721487040L, 558000200519L, 557298871961L, 560494637373L, 524035298556L, 43150564863L, 556977191808L, 44876223761L, 557785081684L, 45807836026L, 559414402679L, 530642572216L, 558995014808L, 540326247806L, 551581846667L, 559036247987L, 561559174409L, 44786294593L};
        Long[] 笔记本电脑 = new Long[]{531852400093L, 35079179932L, 524457097357L, 544138732962L, 556042454257L, 45823184157L, 558521690820L, 550929096291L, 542902453642L, 557859888867L, 45590948868L, 551692086509L, 553755084428L, 534412568248L, 548803150740L, 555015357328L, 544991430888L, 555111641000L, 545383265702L, 39359424898L};


        ArrayList<Object> list = new ArrayList<>();
        list.addAll(Arrays.asList(卫衣));
        list.addAll(Arrays.asList(T恤));
        list.addAll(Arrays.asList(外套));
        list.addAll(Arrays.asList(连衣裙));
        list.addAll(Arrays.asList(披风));
        list.addAll(Arrays.asList(豆奶粉));
        list.addAll(Arrays.asList(奶片));
        list.addAll(Arrays.asList(糖果));
        list.addAll(Arrays.asList(儿童椅));
        list.addAll(Arrays.asList(沙发));
        list.addAll(Arrays.asList(保温奶瓶));
        list.addAll(Arrays.asList(电动牙刷));
        list.addAll(Arrays.asList(学步车));
        list.addAll(Arrays.asList(化妆镜));
        list.addAll(Arrays.asList(电动机));
        list.addAll(Arrays.asList(变压器));
        list.addAll(Arrays.asList(床单));
        list.addAll(Arrays.asList(被套));
        list.addAll(Arrays.asList(防护条));
        list.addAll(Arrays.asList(gps充电器));
        list.addAll(Arrays.asList(钢琴));
        list.addAll(Arrays.asList(琉璃));
        list.addAll(Arrays.asList(帆布鞋));
        list.addAll(Arrays.asList(单肩背包));
        list.addAll(Arrays.asList(笔记本电脑));

        System.out.println(list.size());

        List<Map> res = new ArrayList<>();
        for (Object id : list) {
            HashMap hashMap = new HashMap();
            hashMap.put("sid", "6101609f77d30c7d017d07360f264326528448f7eee0e6179742176");
            hashMap.put("type", 2);
            hashMap.put("numIid", id);
            hashMap.put("cid", "");
            res.add(hashMap);
        }

        System.out.println(JSON.toJSONString(res));
    }



    private static List getItemIdsByKeyWords(String key) {
        String gbk = URLEncoder.encode(key);
        String content = Http.sendGet("http://data.p4psearch.1688.com/data/ajax/search_get_ldp_supplyoffer_html.json?beginpage=1&asyncreq=1&keywords=" + gbk + "&sortType=&descendOrder=&province=&city=&priceStart=&priceEnd=&trackid=885688110390753370624704&cosite=baidujj&location=landing_t4&fcatid=&p4pid=1512376884592066135879&se=&bt=&pageid=b79d42870be0c23a1512376884&_=1512376921777");
        JSONObject contentJson = JSON.parseObject(content);

        String p4pAttrs = contentJson.getJSONObject("data").getJSONObject("content").getString("p4pAttrs");
        List<String> result = new ArrayList<>();
        for (String a : p4pAttrs.split(";")) {
            a = a.substring(0, a.indexOf(","));
            result.add(a);
        }
        return result;
    }
}
